package com.yyk.flower;

import java.util.Optional;
import java.util.Scanner;

public class FlowerIndex {
    //准备一个键盘输入对象
    public static final Scanner input = new Scanner(System.in);

    //准备一个存订单对象的数组
    public static FlowerOrder[] orderList = new FlowerOrder[5];

    //让第一开始就初始化5条数据存入数组中
    static {
        orderList[0] = new FlowerOrder("1001", "金色海岸-香槟玫瑰", 10, 318.0, "2016-12-12", "yyk", " ");
        orderList[1] = new FlowerOrder("1002", "初心不变-白芷玫瑰", 10, 318.0, "2016-12-12", "yyk", " ");
        orderList[2] = new FlowerOrder("1003", "金色海岸-香槟玫瑰", 99, 318.0, "2016-12-12", "yyk", " ");
        orderList[3] = new FlowerOrder("1004", "金色海岸-香槟玫瑰", 17, 318.0, "2016-12-12", "yyk", "包脱单");
        orderList[4] = new FlowerOrder("1005", "金色海岸-香槟玫瑰", 189, 318.0, "2016-12-12", "yyk", "真的脱单");
    }


    public static void main(String[] args) {
        outer:
        while (true) {
            System.out.println("========================欢迎光临\"七彩鲜花\"销售管理系统=========================");
            System.out.println("1.查询销售订单");
            System.out.println("2.修改销售订单");
            System.out.println("3.删除销售订单");
            System.out.println("4.系统退出");
            System.out.println("================================================================================");

            System.out.println("请输入您的选择：");
            int choice = input.nextInt();

            switch (choice) {
                case 1:
                    //查询所有订单功能 findAll()
                    findAll();
                    break;
                case 2:
                    //修改订单功能
                    update();
                    break;
                case 3:
                    deleteSale();
                    break;
                default:
                    System.out.println("退出系统");
                    break outer;
            }
        }
    }


    public static void update() {
        System.out.println("请输入销售编号：");
        String uid = input.next();

        boolean flag = false;
        //遍历数组
        for (FlowerOrder order : orderList) {
            if (order.getId().equals(uid)) {
                flag = true;
                break;
            }
        }
        if (flag == false) {
            System.out.println("对不起，您要修改的订单号不存在！");
            return;
        }
        System.out.println("您要修改的订单详细信息如下：");
        System.out.println();

        System.out.println("编号\t鲜花名称\t销售数量\t价格\t销售日期\t销售员\t备注");
        for (FlowerOrder order : orderList) {
            if (order.getId().equals(uid)) {
                System.out.println("" + order.getId() + "\t" + order.getName() + "\t" + order.getNumber() + "\t" + order.getPrice() + "\t" + order.getDate() + "\t" + order.getSalEmp() + "\t" + order.getBeizhu());

            }
        }

        System.out.println("请输入修改后的鲜花名称：");
        String newName = input.next();
        System.out.println("请输入修改后的销售数量：");
        int newNumber = input.nextInt();
        System.out.println("请输入修改后的鲜花价格：");
        double newPrice = input.nextDouble();
        System.out.println("请输入修改后的销售日期：(yyyy-MM-dd)");
        String newDate = input.next();
        System.out.println("请输入修改后的销售员：");
        String newSalEmp = input.next();
        System.out.println("请输入修改后的备注：");
        String newDecript = input.next();

        //封装对象
        FlowerOrder order = new FlowerOrder(uid, newName, newNumber, newPrice, newDate, newSalEmp, newDecript);

        System.out.println("您确定压迫修改吗？(y/n)");
        String conf = input.next();
        if ("y".equals(conf)) {
            //将order替换数组中id为uid的对象元素
            for (int i = 0; i < orderList.length; i++) {
                FlowerOrder fo = orderList[i];
                if (fo.getId().equals(order.getId())) {
                    orderList[i] = order;
                    break;
                }
            }
        }

    }


    public static void findAll() {
        //打印表头
        System.out.println("编号\t鲜花名称\t销售数量\t价格\t销售日期\t销售员\t备注");
        //打印查询到的5条数据
        for (FlowerOrder flowerOrder : orderList) {
            System.out.println("" + flowerOrder.getId() + "\t" + flowerOrder.getName() + "\t" + flowerOrder.getNumber() + "\t" + flowerOrder.getPrice() + "\t" + flowerOrder.getDate() + "\t" + flowerOrder.getSalEmp() + "\t" + flowerOrder.getBeizhu());
        }
        System.out.println();
        System.out.println();
    }


    private static void deleteSale() {
        System.out.println("请输入要删除的订单编号：");
        String uid = input.next();
        boolean flag = false;
        for (FlowerOrder order : orderList) {
            if (order.getId().equals(uid)) {
                flag = true;
                break;
            }
        }
        if (!flag) {
            System.out.println("对不起，没有您要删除的订单编号！");
            return;
        }
        System.out.println("您要删除的订单详细详细信息如下：");
        System.out.println();
        System.out.println("编号      鲜花名称       销售数量  价格     销售日期   销售员   备注");
        int index = 0;
        for (int i = 0; i < orderList.length; i++) {
            if (orderList[i].getId().equals(uid)) {
                System.out.println(orderList[i].getId() + "\t" + orderList[i].getName() + "\t" + orderList[i].getNumber() + "\t" + orderList[i].getPrice() + "\t" + orderList[i].getDate() + "\t" + orderList[i].getSalEmp() + "\t" + orderList[i].getBeizhu());
                index = i;
            }
        }

        System.out.println("您确定修改吗？(y/n)");
        String yn = input.next();
        if ("y".equals(yn)) {
            FlowerOrder[] newFlower = new FlowerOrder[orderList.length - 1];
            for (int i = 0; i < orderList.length; i++) {
                if (i < index) {
                    newFlower[index] = orderList[i];
                    index++;
                } else if (i > index) {
                    newFlower[index] = orderList[i];
                    index++;
                }
            }
            orderList = newFlower;
            System.out.println("删除成功");
            System.out.println();
            System.out.println();
            findAll();
        }
    }


}

